﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Drawing.Imaging;
using System.Web.UI.HtmlControls;
using System.Data;

namespace Phoenix
{
    public partial class Contactus : System.Web.UI.Page
    {
        ResourceManager rm;
        CultureInfo ci;
        Phoenix.AppCode.Entities.Contact contact = new Phoenix.AppCode.Entities.Contact();
        Phoenix.AppCode.Entities.Article home = new Phoenix.AppCode.Entities.Article();
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie fenix_lang = Request.Cookies["Culture"];
            if (fenix_lang == null)
            {
                fenix_lang = new HttpCookie("Culture", "en-US");
                fenix_lang.Expires = DateTime.Now.AddHours(24);
                Response.Cookies.Add(fenix_lang);
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                Load_Contact(ci);
                LoadDB_US();
                LoadContactUS();
            }
            else
            {
                if (fenix_lang.Value == "vi-VN")
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Contact(ci);
                    LoadDB_VN();
                    LoadContactVN();
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Contact(ci);
                    LoadDB_US();
                    LoadContactUS();
                }
            }
            if (!IsPostBack)
            {
                Session["captcha"] = null;
                CreateCaptcha();

                lblMessage.Text = "";
            }
        }
        private void LoadContactVN()
        {
            DataTable dt = Phoenix.AppCode.Bussiness.CorpInfoService.CorpInfoList();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                lb_namect.Text = dr["CorpName"].ToString();
                lb_address.Text = dr["Address"].ToString();
                lb_phone.Text = dr["Phone"].ToString();
                lb_fax.Text = dr["Fax"].ToString();
                lb_mail.Text = dr["Email"].ToString();
            }
        }
        private void LoadContactUS()
        {
            DataTable dt = Phoenix.AppCode.Bussiness.CorpInfoService.CorpInfoList();
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[1];
                lb_namect.Text = dr["CorpName"].ToString();
                lb_address.Text = dr["Address"].ToString();
                lb_phone.Text = dr["Phone"].ToString();
                lb_fax.Text = dr["Fax"].ToString();
                lb_mail.Text = dr["Email"].ToString();
            }
        }
        public void Load_Contact(CultureInfo ci)
        {
            string cont_contact = rm.GetString("cont_contact", ci);
            string cont_fullname = rm.GetString("cont_fullname", ci);
            string cont_email = rm.GetString("cont_email", ci);
            string cont_sub = rm.GetString("cont_sub", ci);
            string cont_con = rm.GetString("cont_con", ci);
            string cont_captcha = rm.GetString("cont_captcha", ci);
            string cont_bt_submit = rm.GetString("cont_bt_submit", ci);
            f_contact.Text = cont_contact;
            lb_fname.Text = cont_fullname;
            lb_email.Text = cont_email;
            lb_subject.Text = cont_sub;
            lb_cont.Text = cont_con;
            lb_captcha.Text = cont_captcha;
            bt_submit.Text = cont_bt_submit;
        }
        protected void imbReload_Click(object sender, ImageClickEventArgs e)
        {
            Session["captcha"] = null;
            CreateCaptcha();
            lblMessage.Text = "";
        }
        private void CreateCaptcha()
        {
            //string[] fonts = { "Arial Black", "Lucida Sans Unicode", "Comic Sans MS" };
            string[] fonts = { "Arial", "Helvetica", "sans - serif" };
            const byte LENGTH = 4;
            // chuỗi để lấy các kí tự sẽ sử dụng cho captcha
            const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//abcdefghijklmnopqrstuvwxyz";
            using (Bitmap bmp = new Bitmap(100, 30))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    // Tạo nền cho ảnh dạng sóng
                    HatchBrush brush = new HatchBrush(HatchStyle.OutlinedDiamond, Color.LightGreen, Color.White);
                    g.FillRegion(brush, g.Clip);
                    // Lưu chuỗi captcha trong quá trình tạo
                    StringBuilder strCaptcha = new StringBuilder();
                    Random rand = new Random();
                    for (int i = 0; i < LENGTH; i++)
                    {
                        // Lấy kí tự ngẫu nhiên từ mảng chars
                        string str = chars[rand.Next(chars.Length)].ToString();
                        strCaptcha.Append(str);

                        // Tạo font với tên font ngẫu nhiên chọn từ mảng fonts
                        //Font font = new Font(fonts[rand.Next(fonts.Length)], 12, FontStyle.Strikeout | FontStyle.Italic);
                        Font font = new Font(fonts[rand.Next(fonts.Length)], 15, FontStyle.Bold);
                        // Lấy kích thước của kí tự
                        SizeF size = g.MeasureString(str, font);
                        // Vẽ kí tự đó ra ảnh tại vị trí tăng dần theo i, vị trí top ngẫu nhiên
                        //g.DrawString(str, font, Brushes.Chocolate, i * size.Width + 3, rand.Next(2, 10));
                        g.DrawString(str, font, Brushes.Green, i * size.Width + 1, rand.Next(3, 3));
                        font.Dispose();
                    }
                    // Lưu captcha vào session
                    Session["captcha"] = strCaptcha.ToString();
                    // Lưu ảnh vào thư mục captcha với tên ảnh dựa theo IP
                    string path = "Content/Frontend/img/Captcha/" + Request.UserHostAddress + ".gif";//".png"
                    //bmp.Save(Server.MapPath("") + "/" + path, ImageFormat.Gif);
                    bmp.Save(Server.MapPath("") + "/" + path, ImageFormat.Gif);//ImageFormat.Gif
                    imgCaptcha.ImageUrl = path;
                }
            }
        }

        protected void bt_submit_Click(object sender, EventArgs e)
        {
            if (txt_captcha.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase))
            {
                lblMessage.Text = "";
                try
                {
                    contact.FullName = txt_fullname.Text;
                    contact.Email = txt_email.Text;
                    contact.Subject = txt_subject.Text;
                    contact.Content = txt_content.Text;
                    Phoenix.AppCode.Bussiness.ContactService.Create(contact);
                    Response.Redirect("Contactus.aspx");
                }
                catch
                {
                    Response.Redirect("Contactus.aspx");
                }
            }
            else
            {
                lblMessage.Text = "Captcha Incorrect";

            }
        }
        private void LoadDB_VN()
        {
            int id = 1;
            string lang = "vi-VN";
            home.ID_ArticleCategory = id;
            home.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleLoadService(home);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                string dtitle = dr["TitleName"].ToString();
                string htitle = dr["TitleHead"].ToString();
                string des = dr["DescriptionHead"].ToString();
                string key = dr["KeywordHead"].ToString();
                HtmlHead headTag = (HtmlHead)this.Header;
                if (htitle.Length < 2)
                {
                    headTag.Title = dtitle;
                }
                else
                {
                    headTag.Title = htitle;
                }
                HtmlMeta PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "description";
                PagemetaTag.Content = des;
                headTag.Controls.Add(PagemetaTag);

                PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "keywords";
                PagemetaTag.Content = key;
                headTag.Controls.Add(PagemetaTag);
            }
        }
        private void LoadDB_US()
        {
            int id = 1;
            string lang = "en-US";
            home.ID_ArticleCategory = id;
            home.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleLoadService(home);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                string dtitle = dr["TitleName"].ToString();
                string htitle = dr["TitleHead"].ToString();
                string des = dr["DescriptionHead"].ToString();
                string key = dr["KeywordHead"].ToString();
                HtmlHead headTag = (HtmlHead)this.Header;
                if (htitle.Length < 2)
                {
                    headTag.Title = dtitle;
                }
                else
                {
                    headTag.Title = htitle;
                }
                HtmlMeta PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "description";
                PagemetaTag.Content = des;
                headTag.Controls.Add(PagemetaTag);

                PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "keywords";
                PagemetaTag.Content = key;
                headTag.Controls.Add(PagemetaTag);
            }
        }
    }
}